<%-- 
    Document   : mantedoctor
    Created on : Aug 19, 2013, 10:29:35 AM
    Author     : IK
--%>


<%@page import="java.text.SimpleDateFormat"%>
<%@page import="edu.capex.farmacus.encapsulates.Medicos"%>
<%@page import="edu.capex.farmacus.Manager.CentrosmedicosManager"%>
<%@page import="edu.capex.farmacus.encapsulates.Centrosmedicos"%>
<%@page import="edu.capex.farmacus.Manager.MedicosManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    Medicos medicoAMod = null;
    SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy/MM/dd");

    if (request.getParameter("nombre") != null && request.getParameter("fechaNacimiento") != null
            && request.getParameter("consultorio") != null && request.getParameter("centroMedico") != null && request.getParameter("especialidad") != null
            && request.getParameter("id") != null && request.getParameter("moid") == null) {

        //Medicos medico = new Medicos(new Integer(request.getParameter("id")));
        Medicos medico = new MedicosManager().findMedicos(new Integer(request.getParameter("id")));
        boolean existe = false;

        if (medico == null) {
            medico = new Medicos();
        } else {
            existe = true;
        }

        medico.setId(new Integer(request.getParameter("id")));
        medico.setFechaNacimiento(yyyyMMdd.parse(request.getParameter("fechaNacimiento")));
        medico.setConsultorio(request.getParameter("consultorio"));
        medico.setCentroMedico(new Integer(request.getParameter("centroMedico")));
        medico.setNombre(request.getParameter("nombre"));
        medico.setEspecialidad(request.getParameter("especialidad"));

        if (existe) {
            new MedicosManager().edit(medico);
        } else {
            new MedicosManager().create(medico);
        }

    } else if (request.getParameter("moid") != null && request.getParameter("modificar").equalsIgnoreCase("modificar")) {
        medicoAMod = new MedicosManager().findMedicos(new Integer(request.getParameter("moid")));
    } else if (request.getParameter("reid") != null && request.getParameter("eliminar").equalsIgnoreCase("eliminar")) {
        new MedicosManager().destroy(new Integer(request.getParameter("reid")));
    }

%>
<html>
    <head>
        <%@include file="/resources/header.jsp" %>
        <script src="../js/bsa.js"></script>
    </head>
    <%@include file="/resources/header1.jsp" %>
    <body class="preview" data-spy="scroll" data-target=".subnav" data-offset="80">

        <div class="container">
            <!-- Typography
            ================================================== -->
            <section id="typography">
                
                <%@include file="/admin/supervisores/menuadm.jsp" %>

                <h2>Mantenimiento Medico </h2>

                <section id="forms">

                    <form class="form-horizontal well" method="POST" id="myForm">
                        <fieldset>

                            <%if (medicoAMod != null) {%>

                            <input type="hidden" name="id" value="<%=medicoAMod.getId()%>"/>


                            <div class="control-group">
                                <label class="control-label" for="input01">Nombre</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" data-required name="nombre" value="<%=medicoAMod.getNombre()%>" id="input01">
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Fecha Nacimiento</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" name="fechaNacimiento" value="<%=yyyyMMdd.format(medicoAMod.getFechaNacimiento())%>" data-required id="fecha" >
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Consultorio</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" data-required name="consultorio" value="<%=medicoAMod.getConsultorio()%>" id="input01">
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Centro Medico</label>
                                <div class="controls">
                                    <select name="centroMedico" class="input-xlarge">
                                        <%
                                            for (Centrosmedicos temp : new CentrosmedicosManager().findCentrosmedicosEntities()) {
                                                if (medicoAMod.getCentroMedico() == temp.getId()) {
                                        %>
                                        <option value="<%=temp.getId()%>" selected><%=temp.getNombreCentro()%></option>
                                        <%} else {%>
                                        <option value="<%=temp.getId()%>"><%=temp.getNombreCentro()%></option>
                                        <%}%>
                                        <%}%>
                                    </select>
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Especialidad</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" data-required name="especialidad" value="<%=medicoAMod.getEspecialidad()%>" id="input01">
                                </div>
                            </div>


                            <div class="form-actions">
                                <button type="submit" class="btn btn-primary" name="modificar">Modificar</button>
                                <button type="reset" class="btn">Cancelar</button>
                            </div>

                            <%} else {%>

                            <input type="hidden" name="id" value="<%=new MedicosManager().nextId()%>"/>
                            <div class="control-group">
                                <label class="control-label" for="input01">Nombre</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" data-required name="nombre" id="input01">
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Fecha Nacimiento</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" name="fechaNacimiento" data-required id="fecha" >
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Consultorio</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" data-required name="consultorio" id="input01">
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Centro Medico</label>
                                <div class="controls">
                                    <select name="centroMedico" class="input-xlarge">
                                        <%
                                            for (Centrosmedicos temp : new CentrosmedicosManager().findCentrosmedicosEntities()) {
                                        %>
                                        <option value="<%=temp.getId()%>"><%=temp.getNombreCentro()%></option>
                                        <%}%>
                                    </select>
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Especialidad</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" data-required name="especialidad" id="input01">
                                </div>
                            </div>

                            <div class="form-actions">
                                <button type="submit" class="btn btn-primary">Agregar</button>
                                <button type="reset" class="btn">Cancelar</button>
                            </div>
                            <%}%>
                        </fieldset>

                    </form>
                    <table id="myTable" class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th>
                                    Nombre
                                </th>
                                <th filter-type="ddl">
                                    Especialidad
                                </th>
                                <th>
                                    Fecha de Nacimiento
                                </th>
                                <th>
                                    Consultorio
                                </th>
                                <th filter-type="ddl">
                                    Centro Medico
                                </th>
                                <th filter="false">
                                    Modificar
                                </th>
                                <th filter="false">
                                    Eliminar
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <%
                                for (Medicos temp : new MedicosManager().findMedicosEntities()) {
                            %>
                            <tr>
                                <td>
                                    <%=temp.getNombre()%>
                                </td>
                                <td>
                                    <%=temp.getEspecialidad()%>
                                </td>
                                <td>
                                    <%=yyyyMMdd.format(temp.getFechaNacimiento())%>
                                </td>
                                <td>
                                    <%=temp.getConsultorio()%>
                                </td>
                                <td>
                                    <%=new CentrosmedicosManager().findCentrosmedicos(temp.getCentroMedico()).getNombreCentro()%>
                                </td>
                                <td>
                                    <form action="#" method="POST">
                                        <center>
                                            <input type="hidden" name="moid" value="<%=temp.getId()%>"/>
                                            <input type="submit" name="modificar" value="Modificar" class="btn"/>
                                        </center>
                                    </form>
                                </td>
                                <td>
                                    <form action="#" method="POST">
                                        <center>
                                            <input type="hidden" name="reid" value="<%=temp.getId()%>"/>
                                            <input type="submit" name="eliminar" value="Eliminar" class="btn"/>
                                        </center>
                                    </form>
                                </td>
                            </tr>
                            <%}%>
                        </tbody>
                    </table> 

                    <%@include file="/resources/footer1.jsp" %>
                </section>
        </div>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#myTable").tableFilter();
                $("#fecha").datepicker();
                $('#myForm').validate({
                    onKeyup: true,
                    eachValidField: function() {

                        $(this).closest('div').removeClass('error').addClass('success');
                    },
                    eachInvalidField: function() {

                        $(this).closest('div').removeClass('success').addClass('error');
                    }
                });

            });
        </script>
    </body>
</html>

